package com.wawei.seeds.services;

import android.content.Context;
import android.os.Handler;

import com.wawei.seeds.model.BookingModel;
import com.wawei.seeds.model.HeroInfo;
import com.wawei.seeds.model.WalnutMainEvent;
import com.wawei.seeds.services.remote.MainServiceRestfulWrapper;
import com.wawei.seeds.services.remote.ReservationServiceRestfulService;
import com.wawei.seeds.services.remote.ReservationServiceRestfulWrapper;
import com.wawei.seeds.services.remote.WalnutMainServiceRestfulService;
import com.wawei.seeds.utils.NetBase;
import com.wawei.seeds.utils.RetrofitNetworkService;
import com.wawei.seeds.utils.WalnutNotifyHandler;

import java.util.ArrayList;
import java.util.List;

import retrofit.Callback;
import retrofit.RestAdapter;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 *
 * Created by t27891 on 15/6/20.
 */
public class WalnutMainServiceHandler {
    private Context context;

    public WalnutMainServiceHandler(Context context) {
        this.context = context;
    }

    public void retrieveHeroEvents(RestAdapter restAdapter, int count,
                                   final Handler handler, final Integer handlerTag) {
        List<WalnutMainEvent> items = new ArrayList<>(0);
        WalnutMainEvent item;
        switch (handlerTag.intValue()) {
            case WalnutNotifyHandler.LOAD_NEW_ACHIEVE_DATA:
                RetrofitNetworkService.handleSuccess(handler, handlerTag, null);
                break;
            case WalnutNotifyHandler.LOAD_OLD_ACHIEVE_DATA:
                // RetrofitNetworkService.handleSuccess(handler, handlerTag, null);
                WalnutMainServiceRestfulService service = restAdapter.create(WalnutMainServiceRestfulService.class);
                service.getAllMainServices(count, new Callback<MainServiceRestfulWrapper>() {
                    @Override
                    public void success(MainServiceRestfulWrapper mainServiceRestfulWrapper, Response response) {
                        List<WalnutMainEvent> entities = mainServiceRestfulWrapper.getItems();
                        if (null != entities && !entities.isEmpty()) {
                            for (WalnutMainEvent entity : entities) {
                                entity.value = "100个核桃";
                            }
                            RetrofitNetworkService.handleSuccess(handler, handlerTag, entities.toArray());
                        } else  {
                            RetrofitNetworkService.handleSuccess(handler, handlerTag, new ArrayList<>(0).toArray());
                        }
                    }

                    @Override
                    public void failure(RetrofitError retrofitError) {
                        RetrofitNetworkService.handleError(handler, handlerTag, retrofitError);
                    }
                });
                break;
            case WalnutNotifyHandler.INIT_ACHIEVE_DATA:
                WalnutMainServiceRestfulService restfulService = restAdapter.create(WalnutMainServiceRestfulService.class);
                restfulService.getAllMainServices(new Callback<MainServiceRestfulWrapper>() {
                    @Override
                    public void success(MainServiceRestfulWrapper mainServiceRestfulWrapper, Response response) {
                        List<WalnutMainEvent> entities = mainServiceRestfulWrapper.getItems();
                        if (null != entities && !entities.isEmpty()) {
                            for (WalnutMainEvent entity : entities) {
                                entity.value = "100个核桃";
                            }
                            RetrofitNetworkService.handleSuccess(handler, handlerTag, entities.toArray());
                        } else  {
                            RetrofitNetworkService.handleSuccess(handler, handlerTag, new ArrayList<>(0).toArray());
                        }
                    }

                    @Override
                    public void failure(RetrofitError retrofitError) {
                        RetrofitNetworkService.handleError(handler, handlerTag, retrofitError);
                    }
                });
                break;
            default:
                break;
        }
    }

    public void retrieveEventDetail(RestAdapter restAdapter, String itemId, String loggedIn,
                                    final Handler handler, final Integer handlerTag) {
        WalnutMainEvent item;
        item = new WalnutMainEvent();
        item.AD = "http://upload.jashnname.com/UploadedFiles/Core/Images/8ae6b53a-2f23-4a61-81cd-c8d6e6058c92.jpg";
        item.title = "淘宝架构的演化过程";
        item.action = "分享";
        item.available = "本月可预约5次";
        item.publisher = "苏妲己";
        item.role = "高级架构师";
        item.detail = "一个粗糙的产品，无法用户看上第一眼看上，那就是失败的。男人喜欢看美女那是人性，人都喜欢美的东西，这更是人性。";
        item.eventId = "";
        item.value = "100个核桃";
        item.status = "已约";
        item.lastUpdate = "";
    }

    public void retrieveEventDetail(RestAdapter restAdapter, String itemId,
                                    final Handler handler, final Integer handlerTag) {
        WalnutMainEvent item;
        item = new WalnutMainEvent();
        item.AD = "http://upload.jashnname.com/UploadedFiles/Core/Images/8ae6b53a-2f23-4a61-81cd-c8d6e6058c92.jpg";
        item.title = "淘宝架构的演化过程";
        item.action = "分享";
        item.available = "本月可预约5次";
        item.publisher = "苏妲己";
        item.role = "高级架构师";
        item.detail = "一个粗糙的产品，无法用户看上第一眼看上，那就是失败的。男人喜欢看美女那是人性，人都喜欢美的东西，这更是人性。";
        item.eventId = "";
        item.value = "100个核桃";
        item.status = "已约";
        item.lastUpdate = "";
    }

    public void reservationEvent(RestAdapter restAdapter, WalnutMainEvent event, int userId,
                                 final Handler handler, final Integer handlerTag) {

        ReservationServiceRestfulService restfulService = restAdapter.create(ReservationServiceRestfulService.class);

        restfulService.reservation(userId, event.eventId, event.publisherId, System.currentTimeMillis() / 1000, 1, "这个参数是什么鬼？",  new Callback<NetBase>() {

            @Override
            public void success(NetBase netBase, Response response) {
                try {
                    if (netBase.getResultCode() == 200) {
                        RetrofitNetworkService.handleSuccess(handler, handlerTag, "ok");
                    } else {
                        RetrofitNetworkService.handleError(handler, handlerTag, null);
                    }
                } catch (Exception e) {
                    RetrofitNetworkService.handleError(handler, handlerTag, null);
                }
            }

            @Override
            public void failure(RetrofitError error) {
                RetrofitNetworkService.handleError(handler, handlerTag, null);
            }
        });
    }

    // 获取当前用户针对当前item的预约状态
    public void getBookingState(RestAdapter restAdapter, final WalnutMainEvent event, int userId,
                                final Handler handler, final Integer handlerTag) {
        ReservationServiceRestfulService restfulService = restAdapter.create(ReservationServiceRestfulService.class);
        restfulService.getUserBookings(userId, new Callback<ReservationServiceRestfulWrapper>() {
            @Override
            public void success(ReservationServiceRestfulWrapper reservationServiceRestfulWrapper, Response response) {
                List<BookingModel> beans = reservationServiceRestfulWrapper.getBookingBeans();
                String state = null;

                if (beans != null && !beans.isEmpty()) {
                    boolean flag = false;
                    for (BookingModel model : beans) {
                        // 首先匹配发布者(提供者)id一样的数据，如果连发布者都不一致的话，就不可能是这笔记录了
                        if (model.providerId.equals(event.publisherId)) {
                            if (model.items != null && !model.items.isEmpty()) {
                                for (WalnutMainEvent item : model.items) {
                                    // 匹配对应的item id，如果能匹配上，赋值对应的状态字符串
                                    if (item.eventId.equals(event.eventId)) {
                                        state = model.bookingState;
                                        flag = true;
                                        break;
                                    }
                                }
                            }
                        }

                        if (flag) {
                            break;
                        }
                    }
                }

                RetrofitNetworkService.handleSuccess(handler, handlerTag, state);
            }

            @Override
            public void failure(RetrofitError error) {
                RetrofitNetworkService.handleError(handler, handlerTag, null);
            }
        });
    }
}
